package com.andy.basics;

import java.io.IOException;

import javax.faces.context.FacesContext;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;

@Component
@Scope("request")
public class HttpUtil {

	public String getRequestParameter(final String name) {
		return FacesContext.getCurrentInstance().getExternalContext().getRequestParameterMap().get(name);
	}

	public String getRemoteUser() {
		return FacesContext.getCurrentInstance().getExternalContext().getRemoteUser();
	}

	public void sendRedirect(String location) throws IOException {
		this.getResponse().sendRedirect(location);
	}

	public void redirect(String location) throws IOException {
		FacesContext.getCurrentInstance().getExternalContext().redirect(location);
	}

	protected HttpServletResponse getResponse() {
		return ((HttpServletResponse) FacesContext.getCurrentInstance().getExternalContext().getResponse());
	}

	protected HttpServletRequest getRequest() {
		return ((HttpServletRequest) FacesContext.getCurrentInstance().getExternalContext().getRequest());
	}
}
